package leetcode.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author wangmaolin1
 * @date 2021/12/29 下午10:06
 */
public class _226翻转二叉树 {

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        if (root.left != null) {
            invertTree(root.left);
        }
        if (root.right != null) {
            invertTree(root.right);
        }
        return root;
    }

    public TreeNode invertTree2(TreeNode root) {
        if (root==null){
            return null;
        }
        Queue<TreeNode>queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            TreeNode left = node.left;
            node.left=node.right;
            node.right=left;
            if (node.left!=null){
                queue.offer(node.left);
            }
            if (node.right!=null){
                queue.offer(node.right);
            }
        }
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(-1);
        root.right = new TreeNode(2);
        TreeNode right = root.right;
        root.right = null;
        System.out.println(right);
    }
}
